import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

class IDPic extends StatefulWidget {
  final File iDFront;
  final File iDReverse;
  final Function getIDFront;
  final Function getIDReverse;
  const IDPic({Key key, this.iDFront, this.iDReverse, this.getIDFront, this.getIDReverse}) : super(key: key);

  @override
  _IDPicState createState() => _IDPicState();
}

class _IDPicState extends State<IDPic> {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          width: setWidth(165),
          height: setWidth(110),
          decoration: BoxDecoration(
              border:
                  Border.all(color: Color(0xffF0F0F0), width: setWidth(0.5))),
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: (){
              _galleryImage(0);
            },
            child: Container(
              width: setWidth(148),
              height: setWidth(92),
              child: widget.iDFront == null
                  ? Image.asset("assets/images/front_id.png", fit: BoxFit.cover)
                  : Image.file(widget.iDFront, fit: BoxFit.cover),
            ),
          ),
        ),
        Container(
          width: setWidth(165),
          height: setWidth(110),
          decoration: BoxDecoration(
              border:
                  Border.all(color: Color(0xffF0F0F0), width: setWidth(0.5))),
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: (){
              _galleryImage(1);
            },
            child: Container(
              width: setWidth(148),
              height: setWidth(92),
              child:widget.iDReverse == null
                  ? Image.asset("assets/images/reverse_id.png", fit: BoxFit.cover): Image.file(widget.iDReverse, fit: BoxFit.cover),
            ),
          ),
        ),
      ],
    );
  }

  //从相册选择图片
  _galleryImage(int index) async {
    await ImagePicker.pickImage(source: ImageSource.gallery).then((value){
      if(value != null){
        switch(index){
          case 0 : widget.getIDFront(value);break;
          case 1 : widget.getIDReverse(value);break;
        }

      }
    });
  }

}
